package com.hbzhou.open.flowcamera.util;

import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * author hbzhou
 * date 2019/12/13 10:49
 */
public class ScreenUtils {
    public static final int NO_ROTATION = 0;        //竖屏，屏幕未旋转
    public static final int ROTATE_90_DEGREES = 1;  //横屏，由'0'这种状态向右上方旋转90度
    public static final int ROTATE_180_DEGREES = 2; //竖屏，由'0'这种状态旋转180度
    public static final int ROTATE_270_DEGREES = 3; //横屏，由'0'这种状态向左上方旋转90度
    /**
     * get Screen Height
     *
     * @param context Context
     * @return int
     */
    public static int getScreenHeight(Context context) {
        DisplayAttributes displayAttributes =
                DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return displayAttributes.height;
    }

    /**
     * get Screen Width
     *
     * @param context Context
     * @return int
     */
    public static int getScreenWidth(Context context) {
        DisplayAttributes displayAttributes =
                DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return displayAttributes.width;
    }

    public static int getFullScreenHeight(Context context) {
        int[] pointXY = getPoint(context);
        return Math.max(pointXY[0], pointXY[1]);
    }

    public static int getFullScreenWidth(Context context) {
        int[] pointXY = getPoint(context);
        return Math.min(pointXY[0], pointXY[1]);
    }

    private static int[] getPoint(Context context) {
        int[] pointXY = new int[2];
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        Point point = new Point();
        display.getSize(point);
        pointXY[0] = point.getPointXToInt();
        pointXY[1] = point.getPointYToInt();
        return pointXY;
    }

    /**
     * 判断是否是竖屏
     * @param context 上下文
     * @return 如果是竖屏则返回true
     */
    public static boolean isVerticalScreen(Context context) {
        int rotation = getScreenRotation(context);
        return rotation % 2 == 0;
    }

    /**
     * 判断是否是横屏
     * @param context 上下文
     * @return 如果是竖屏则返回true
     */
    public static boolean isHorizontalScreen(Context context) {
        int rotation = getScreenRotation(context);
        return rotation % 2 == 1;
    }

    /**
     * 获取屏幕旋转角度
     * @param context 上下文
     * @return 旋转角度
     */
    public static int getScreenRotation(Context context) {
        return DisplayManager.getInstance().getDefaultDisplay(context).get().getRotation();
    }

}
